package ro.tm.uvt.lp.raccoon.catchGame.screens;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;

import ro.tm.uvt.lp.raccoon.SaveTheRaccoon;
import ro.tm.uvt.lp.raccoon.catchGame.entities.Odor;
import ro.tm.uvt.lp.raccoon.catchGame.entities.agents.Dog;
import ro.tm.uvt.lp.raccoon.catchGame.entities.agents.Raccoon;
import ro.tm.uvt.lp.raccoon.catchGame.entities.perceptions.CookieOdor;
import ro.tm.uvt.lp.raccoon.catchGame.entities.perceptions.DogOdor;
import ro.tm.uvt.lp.raccoon.catchGame.entities.perceptions.RaccoonOdor;
import ro.tm.uvt.lp.raccoon.catchGame.entities.physicalObjects.Burrow;
import ro.tm.uvt.lp.raccoon.catchGame.entities.physicalObjects.Food;
import ro.tm.uvt.lp.raccoon.catchGame.entities.physicalObjects.NatureStaticEntity;
import ro.tm.uvt.lp.raccoon.catchGame.environment.Cell;
import ro.tm.uvt.lp.raccoon.catchGame.environment.Environment;

/**
 * Created by user on 1/11/2015.
 */
public class RaccoonGameScreen implements Screen {

    private SaveTheRaccoon game;

    private TiledMap map;
    private OrthogonalTiledMapRenderer mapRenderer;
    private OrthographicCamera camera;
//    private Raccoon raccoon;


    ShapeRenderer sr;

    private Raccoon raccoon;


    private Music raccoonCallingMusic;

    public RaccoonGameScreen(Game game){
        this.game=(SaveTheRaccoon) game;

        raccoonCallingMusic = Gdx.audio.newMusic(Gdx.files.internal("raccoonCalling.mp3"));
        raccoonCallingMusic.setLooping(true);

        map= new TmxMapLoader().load("map/mapUniGrass.tmx");
        mapRenderer = new OrthogonalTiledMapRenderer(map);

        camera = new OrthographicCamera();
        camera.setToOrtho(false, 800, 480);

        Environment.INSTANCE.setHeight(480);
        Environment.INSTANCE.setWidth(800);
        Environment.INSTANCE.setTileHeight(40);
        Environment.INSTANCE.setTileWidth(40);

        Environment.INSTANCE.createNature();
        Environment.INSTANCE.createBurrow();
        Environment.INSTANCE.createFood();
        Environment.INSTANCE.createRaccoon();
        Environment.INSTANCE.createDogs();

////        System.out.println(Environment.INSTANCE.getCells());
        for(Cell cell:Environment.INSTANCE.getCells())
        Gdx.app.debug(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ",cell.toString());


    }

    @Override
    public void show() {
        // start the playback of the background music
        // when the screen is shown
//        raccoonCallingMusic.play();
    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(0,0,0,1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        mapRenderer.setView(camera);
        mapRenderer.render();
        showGrid();

        createNature();
        createBurrow();

        createFood();

        createRaccoon();
        createDogs();

    }

    @Override
    public void resize(int width, int height) {
        camera.viewportHeight = height;
        camera.viewportWidth = width;
        camera.update();
    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void hide() {
        dispose();
    }

    @Override
    public void dispose() {
        raccoonCallingMusic.dispose();

        disposeNature();
        disposeBurrow();
        disposeFood();
        disposeFoodOdor();
        disposeRaccoon();
        disposeDogs();

        sr.dispose();
        mapRenderer.dispose();

        map.dispose();
    }

    /**
     * create grid
     */
    private void showGrid(){
        sr = new ShapeRenderer();
        int tileWidth = map.getProperties().get("tilewidth", Integer.class), tileHeight = map.getProperties().get("tileheight", Integer.class);
        int mapWidth = map.getProperties().get("width", Integer.class) * tileWidth, mapHeight = map.getProperties().get("height", Integer.class) * tileHeight;
        sr.setProjectionMatrix(camera.combined);
        sr.begin(ShapeRenderer.ShapeType.Line);
        for(int x = 0; x < mapWidth; x += tileWidth)
            sr.line(x, 0, x, mapHeight);
        for(int y = 0; y < mapHeight; y += tileHeight)
            sr.line(0, y, mapWidth, y);
        sr.end();
    }

    /**
     * draws all static elements that create the nature: trees, bushes etc
     */
    private void createNature(){
        mapRenderer.getBatch().begin();
        for(NatureStaticEntity nse:Environment.INSTANCE.getNature()){
            nse.draw(mapRenderer.getBatch());
        }
        mapRenderer.getBatch().end();
    }

    /**
     * delete all nature elements
     */
    private void disposeNature(){
        for(NatureStaticEntity nse:Environment.INSTANCE.getNature()){
            nse.getTexture().dispose();
        }
    }

    /**
     *
     */
    private void createBurrow(){
        mapRenderer.getBatch().begin();
        Environment.INSTANCE.getBurrow().draw(mapRenderer.getBatch());
        mapRenderer.getBatch().end();
    }

    /**
     *
     */
    private void disposeBurrow(){
        Environment.INSTANCE.getBurrow().getTexture().dispose();
    }

    /**
     *
     */
    private void createFood(){
        for(Food food:Environment.INSTANCE.getFoods()){
            mapRenderer.getBatch().begin();
            for(CookieOdor co:food.getOdor()){
                co.draw(mapRenderer.getBatch());
            }
            food.draw(mapRenderer.getBatch());
            mapRenderer.getBatch().end();
        }
    }

    /**
     *
     */
    private void disposeFood(){
        for(Food food:Environment.INSTANCE.getFoods()){
            food.getTexture().dispose();
        }
    }

    /**
     *
     */
    private void disposeFoodOdor(){
        for(Food food:Environment.INSTANCE.getFoods()){
            for(CookieOdor co:food.getOdor()){
                co.getTexture().dispose();
            }
        }
    }


    /**
     * create Dogs
     */
    private void createDogs(){
        for(Dog dog:Environment.INSTANCE.getDogs()){
            mapRenderer.getBatch().begin();
            for(DogOdor dogOdor:dog.getOdor()){
                dogOdor.draw(mapRenderer.getBatch());
            }
            dog.draw(mapRenderer.getBatch());
            mapRenderer.getBatch().end();
        }
    }

    /**
     *
     */
    private void disposeDogs(){
        for(Dog dog:Environment.INSTANCE.getDogs()){
            for(DogOdor dogOdor:dog.getOdor()){
                dogOdor.getTexture().dispose();
            }
            dog.getTexture().dispose();
        }
    }


    private void createRaccoon(){
        mapRenderer.getBatch().begin();
        for(RaccoonOdor ro:Environment.INSTANCE.getRaccoon().getOdor()){
            ro.draw(mapRenderer.getBatch());
        }
        Environment.INSTANCE.getRaccoon().draw(mapRenderer.getBatch());
        mapRenderer.getBatch().end();
    }

    private void disposeRaccoon(){
        Environment.INSTANCE.getRaccoon().getTexture().dispose();
        for(RaccoonOdor ro:Environment.INSTANCE.getRaccoon().getOdor()){
            ro.getTexture().dispose();
        }
    }









}
